宏 for_each_process
宏 for_each_process 的功能是:遍历进程(task_struct)
这个宏从指向 &init_task
开始,把指针移到下一个任务(在 linux 底层中,进程被称为任务 task),然后继续,直到又回到 &init_task
(得益于 task_struct
中的 tasks
成员,tasks
是一个 list_head
类型,是一个双向链表,具有循环性)。
// include/linux/sched/signal.h
#define next_task(p) \
list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
// include/linux/rculist.h
/**
* list_entry_rcu - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* This primitive may safely run concurrently with the _rcu list-mutation
* primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
*/
#define list_entry_rcu(ptr, type, member) \
container_of(READ_ONCE(ptr), type, member)